package responsitory;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bean.GoodsDetailBean;
import bean.GoodsModel;
import bean.PayFangShiBean;
import bean.SouSuoMall;
import bean.ZiLeiMuBean;
import interfaces.IShopMallSearch;
import util.volley.BaseErrorListener;
import util.volley.BaseNetRepository;
import util.volley.BaseSuccessReqListener;
import util.volley.ByJsonRequest;
import util.volley.ICompleteListener;

/**
 * 商品
 */
public class ShopMallSearch extends BaseNetRepository implements IShopMallSearch<GoodsModel> {

    final String APIKEY = "100";
    final String GoodsSearch = "By_Product_search";//商品列表
    final String mallSousuo = "By_Product_searchKeywords";//商品搜索
    final String payFangshi = "By_Config_supportPayways";//支付方式
    final String mallxq = "By_Product_detail";//商品详情
    final String leimu = "By_Category_querySubCategory";//子类目查询

    public ShopMallSearch(ICompleteListener iCompleteListener) {
        super(iCompleteListener);
    }

    /**
     * 商品列表
     * @param page_index    当前页码[可选,默认1]
     * @param page_size     每页大小[可选,默认10]
     * @param cate_id       类目id[可选]
     * @param prop_id       一级类目属性id[可选]
     * @param order         排序(d=>'默认',pd=>价格从高到低,pa=>价格从低到高)可选
     * @param keyword       关键词[可选]
     * @param l_price       下限价格[可选]
     * @param r_price       上限价格[可选]
     */
    @Override
    public void shangPinList(String page_index, String page_size, String cate_id, String prop_id, String order, String keyword, String l_price, String r_price) {
        Type type = new TypeToken<GoodsModel>() {
        }.getType();
        String appkey="101";
        Map<String, Object> map = new HashMap<>();
        map.put("page_index", page_index);
        map.put("page_size", page_size);
        map.put("cate_id", cate_id);
        map.put("prop_id", prop_id);
        map.put("order", order);
        map.put("keyword", keyword);
        map.put("l_price", l_price);
        map.put("r_price", r_price);

        (new ByJsonRequest.Builder<GoodsModel>())
                .setTypeVerParamsAndReturnClass(GoodsSearch, appkey, map, type)
                .requestListener(
                        new BaseSuccessReqListener<GoodsModel>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 商品搜索
     * @param keyword 关键词
     */
    @Override
    public void getMallSouSuo(String keyword) {
        Type type = new TypeToken<List<SouSuoMall>>() {
        }.getType();
        Map<String, Object> map = new HashMap<>();
        map.put("keyword", keyword);
        (new ByJsonRequest.Builder<List<SouSuoMall>>())
                .setTypeVerParamsAndReturnClass(mallSousuo, APIKEY, map, type)
                .requestListener(
                        new BaseSuccessReqListener<List<SouSuoMall>>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void payFangShi() {
        Type type = new TypeToken<List<PayFangShiBean>>() {
        }.getType();
        Map<String, Object> map = new HashMap<>();
        (new ByJsonRequest.Builder<List<PayFangShiBean>>())
                .setTypeVerParamsAndReturnClass(payFangshi, APIKEY, map, type)
                .requestListener(
                        new BaseSuccessReqListener<List<PayFangShiBean>>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 商品详情
     * @param id
     */
    @Override
    public void mallXiangQing(String id) {
        Type type = new TypeToken<GoodsDetailBean>() {
        }.getType();
        String apk="102";
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        (new ByJsonRequest.Builder<GoodsDetailBean>())
                .setTypeVerParamsAndReturnClass(mallxq, apk, map, type)
                .requestListener(
                        new BaseSuccessReqListener<GoodsDetailBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 子目录查询
     * @param cate_id   类目id
     */
    @Override
    public void LeiMuQuery(String cate_id) {
        Type type = new TypeToken<List<ZiLeiMuBean>>() {
        }.getType();
        String apk="101";
        Map<String, Object> map = new HashMap<>();
        map.put("cate_id", cate_id);
        (new ByJsonRequest.Builder<List<ZiLeiMuBean>>())
                .setTypeVerParamsAndReturnClass(leimu, apk, map, type)
                .requestListener(
                        new BaseSuccessReqListener<List<ZiLeiMuBean>>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }


}
